package No0227_basic_calculator_ii

func calculate(s string) int {
	var res int = 0
	var l = len(s)
	var nums = make([]int, 0)
	var opt = '+'
	for i := 0; i < l; i++ {
		if s[i] >= '0' && s[i] <= '9' { //是数字
			var tmpNum int = int(s[i]) - int('0')
			for i < l-1 && s[i+1] >= '0' && s[i+1] <= '9' {
				tmpNum = tmpNum*10 + (int(s[i+1]) - int('0'))
				i++
			}
			if opt == '+' {
				nums = append(nums, tmpNum)
			} else if opt == '-' {
				nums = append(nums, -1*tmpNum)
			} else if opt == '*' || opt == '/' {
				t := nums[len(nums)-1]
				nums = nums[:len(nums)-1]
				if opt == '*' {
					nums = append(nums, t*tmpNum)
				} else {
					nums = append(nums, t/tmpNum)
				}
			}
		} else if s[i] == '+' {
			opt = '+'
		} else if s[i] == '-' {
			opt = '-'
		} else if s[i] == '*' {
			opt = '*'
		} else if s[i] == '/' {
			opt = '/'
		}
	}
	l2 := len(nums)
	for i := 0; i < l2; i++ {
		res += nums[i]
	}
	return res
}

var Solution func(string) int = calculate
